﻿@page "/steps"

@using RadzenBlazorDemos.Data
@using RadzenBlazorDemos.Models.Northwind
@using Microsoft.EntityFrameworkCore

@inject NorthwindContext dbContext

<RadzenExample Name="Steps">
<div class="row">
    <div class="col-xl-6">
        <RadzenSteps Change=@OnChange>
            <Steps>
                <RadzenStepsItem Text="Customers">
                    <h3>Select Customer</h3>
                    <RadzenDataGrid ColumnWidth="200px" AllowFiltering="true" AllowPaging="true" AllowSorting="true" Data="@customers" TItem="Customer" @bind-Value="@selectedCustomers">
                        <Columns>
                            <RadzenDataGridColumn TItem="Customer" Property="CustomerID" Title="Customer ID" />
                            <RadzenDataGridColumn TItem="Customer" Property="CompanyName" Title="Company Name" />
                            <RadzenDataGridColumn TItem="Customer" Property="ContactName" Title="Contact Name" />
                            <RadzenDataGridColumn TItem="Customer" Property="ContactTitle" Title="Contact Title" />
                            <RadzenDataGridColumn TItem="Customer" Property="Address" Title="Address" />
                            <RadzenDataGridColumn TItem="Customer" Property="City" Title="City" />
                            <RadzenDataGridColumn TItem="Customer" Property="Region" Title="Region" />
                            <RadzenDataGridColumn TItem="Customer" Property="PostalCode" Title="Postal Code" />
                            <RadzenDataGridColumn TItem="Customer" Property="Country" Title="Country" />
                            <RadzenDataGridColumn TItem="Customer" Property="Phone" Title="Phone" />
                            <RadzenDataGridColumn TItem="Customer" Property="Fax" Title="Fax" />
                        </Columns>
                    </RadzenDataGrid>
                </RadzenStepsItem>
                <RadzenStepsItem Text="Orders" Disabled="@(selectedCustomers == null || selectedCustomers != null && !selectedCustomers.Any())">
                    <h3>Select Order</h3>
                    <RadzenDataGrid ColumnWidth="200px" PageSize="2" AllowFiltering="true" AllowPaging="true" AllowSorting="true" 
                                Data="@(selectedCustomers != null && selectedCustomers.Any() ? orders.Where(o => o.CustomerID == selectedCustomers[0].CustomerID) : Enumerable.Empty<Order>())" TItem="Order" @bind-Value="@selectedOrders">
                        <Columns>
                            <RadzenDataGridColumn Width="100px" TItem="Order" Property="OrderID" Title="Order ID" />
                            <RadzenDataGridColumn Width="200px" TItem="Order" Property="Customer.CompanyName" Title="Customer" />
                            <RadzenDataGridColumn Width="150px" TItem="Order" Property="Employee.LastName" Title="Employee">
                                <Template Context="order">
                                    <div>@order.Employee?.LastName</div>
                                    <RadzenImage Path="@order.Employee?.Photo" />
                                </Template>
                            </RadzenDataGridColumn>
                            <RadzenDataGridColumn TItem="Order" Property="OrderDate" Title="Order Date">
                                <Template Context="order">
                                    @String.Format("{0:d}", order.OrderDate)
                                </Template>
                            </RadzenDataGridColumn>
                            <RadzenDataGridColumn TItem="Order" Property="RequiredDate" Title="Required Date">
                                <Template Context="order">
                                    @String.Format("{0:d}", order.RequiredDate)
                                </Template>
                            </RadzenDataGridColumn>
                            <RadzenDataGridColumn TItem="Order" Property="ShippedDate" Title="Shipped Date">
                                <Template Context="order">
                                    @String.Format("{0:d}", order.ShippedDate)
                                </Template>
                            </RadzenDataGridColumn>
                            <RadzenDataGridColumn TItem="Order" Property="Freight" Title="Freight">
                                <Template Context="order">
                                    @String.Format(new System.Globalization.CultureInfo("en-US"), "{0:C}", order.Freight)
                                </Template>
                            </RadzenDataGridColumn>
                            <RadzenDataGridColumn TItem="Order" Property="ShipName" Title="Ship Name" />
                            <RadzenDataGridColumn TItem="Order" Property="ShipAddress" Title="Ship Address" />
                            <RadzenDataGridColumn TItem="Order" Property="ShipCity" Title="Ship City" />
                            <RadzenDataGridColumn TItem="Order" Property="ShipRegion" Title="Ship Region" />
                            <RadzenDataGridColumn TItem="Order" Property="ShipPostalCode" Title="Ship Postal Code" />
                            <RadzenDataGridColumn TItem="Order" Property="ShipCountry" Title="Ship Country" />
                        </Columns>
                    </RadzenDataGrid>
                </RadzenStepsItem>
                <RadzenStepsItem Text="Order Details" Disabled="@(selectedOrders == null || selectedOrders != null && !selectedOrders.Any())">
                    <RadzenDataGrid AllowFiltering="true" AllowPaging="true" AllowSorting="true"
                                Data="@(selectedOrders != null && selectedOrders.Any() ? orderDetails.Where(o => o.OrderID == selectedOrders[0].OrderID) : Enumerable.Empty<OrderDetail>())" TItem="OrderDetail" ColumnWidth="200px">
                        <Columns>
                            <RadzenDataGridColumn TItem="OrderDetail" Property="Product.ProductName" Title="Product" />
                            <RadzenDataGridColumn TItem="OrderDetail" Property="Quantity" Title="Quantity" />
                            <RadzenDataGridColumn TItem="OrderDetail" Property="Discount" Title="Discount" FormatString="{0:P}" />
                        </Columns>
                    </RadzenDataGrid>
                </RadzenStepsItem>
            </Steps>
        </RadzenSteps>
    </div>
    <div class="col-xl-6">
        <EventConsole @ref=@console />
    </div>
</div>
</RadzenExample>

@code {
    EventConsole console;
    IEnumerable<Customer> customers;
    IEnumerable<Order> orders;
    IEnumerable<OrderDetail> orderDetails;

    IList<Customer> selectedCustomers;
    IList<Order> selectedOrders;

    protected override void OnInitialized()
    {
        customers = dbContext.Customers.ToList();
        orders = dbContext.Orders.Include("Customer").Include("Employee").ToList();
        orderDetails = dbContext.OrderDetails.Include("Product").ToList();
    }
    void OnChange(int index)
    {
        console.Log($"Step with index {index} was selected.");
    }
}